package com.ruoyi.system.controller;

import cn.hutool.core.swing.clipboard.ClipboardUtil;
import cn.xiaosiyuan.util.genId.PKGenerator;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.ImageUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TbPersonalPictureManage;
import com.ruoyi.system.service.ITbPersonalPictureManageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

/**
 * 图片保存Controller
 * 
 * @author xiaosiyuan
 * @date 2021-11-15
 */
@RestController
@RequestMapping("/system/pictureManage")
public class TbPersonalPictureManageController extends BaseController
{
    @Autowired
    private ITbPersonalPictureManageService tbPersonalPictureManageService;

    /**
     * 查询图片保存列表
     */
    @PreAuthorize("@ss.hasPermi('system:pictureManage:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbPersonalPictureManage tbPersonalPictureManage)
    {
        startPage();
        List<TbPersonalPictureManage> list = tbPersonalPictureManageService.selectTbPersonalPictureManageList(tbPersonalPictureManage);
        return getDataTable(list);
    }

    /**
     * 导出图片保存列表
     */
    @PreAuthorize("@ss.hasPermi('system:pictureManage:export')")
    @Log(title = "图片保存", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbPersonalPictureManage tbPersonalPictureManage)
    {
        List<TbPersonalPictureManage> list = tbPersonalPictureManageService.selectTbPersonalPictureManageList(tbPersonalPictureManage);
        ExcelUtil<TbPersonalPictureManage> util = new ExcelUtil<TbPersonalPictureManage>(TbPersonalPictureManage.class);
        return util.exportExcel(list, "图片保存数据");
    }

    /**
     * 获取图片保存详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:pictureManage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(tbPersonalPictureManageService.selectTbPersonalPictureManageById(id));
    }

    /**
     * 新增图片保存
     */
    @PreAuthorize("@ss.hasPermi('system:pictureManage:add')")
    @Log(title = "图片保存", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbPersonalPictureManage tbPersonalPictureManage)
    {
        tbPersonalPictureManage.setId(PKGenerator.newId());
        return toAjax(tbPersonalPictureManageService.insertTbPersonalPictureManage(tbPersonalPictureManage));
    }

    /**
     * 修改图片保存
     */
    @PreAuthorize("@ss.hasPermi('system:pictureManage:edit')")
    @Log(title = "图片保存", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbPersonalPictureManage tbPersonalPictureManage)
    {
        return toAjax(tbPersonalPictureManageService.updateTbPersonalPictureManage(tbPersonalPictureManage));
    }

    /**
     * 删除图片保存
     */
    @PreAuthorize("@ss.hasPermi('system:pictureManage:remove')")
    @Log(title = "图片保存", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tbPersonalPictureManageService.deleteTbPersonalPictureManageByIds(ids));
    }


    // 复制文件
    @PreAuthorize("@ss.hasPermi('system:pictureManage:copyFile')")
    @Log(title = "复制文件", businessType = BusinessType.DELETE)
    @PostMapping("/copyFile")
    public AjaxResult copyFile() {
        int rs = 0;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Map<String, String[]> map = request.getParameterMap();
        String id = request.getParameter("id");
        String path = "";
        if (id != null || !Objects.equals(id, "")) {
            TbPersonalPictureManage entity = tbPersonalPictureManageService.selectTbPersonalPictureManageById(id);

            path = entity.getAttachArea().replace("/profile/upload", RuoYiConfig.getUploadPath().toString());

        }


        Image image = null;
        try {
            image = ImageIO.read(new File(path));
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (image != null) {
            String headLess = System.getProperty("java.awt.headless");

            ClipboardUtil.setImage(image);
            rs = 200;
        }


        return toAjax(rs);
    }

}
